home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH5 / EMAGA5 / common / server / missionLoad.cs < prev    next >
Text File  |  2005-11-23  |  4KB  |  148 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------------------
  7. // Server mission loading
  8. //-----------------------------------------------------------------------------
  9.  
  10. // On every mission load except the first, there is a pause after
  11. // the initial mission info is downloaded to the client.
  12. $MissionLoadPause = 5000;
  13.  
  14. //-----------------------------------------------------------------------------
  15.  
  16. function loadMission( %missionName, %isFirstMission ) 
  17. {
  18.    endMission();
  19.    echo("*** LOADING MISSION: " @ %missionName);
  20.    echo("*** Stage 1 load");
  21.  
  22.    // Reset all of these
  23.    clearCenterPrintAll();
  24.    clearBottomPrintAll();
  25.  
  26.    // increment the mission sequence (used for ghost sequencing)
  27.    $missionSequence++;
  28.    $missionRunning = false;
  29.    $Server::MissionFile = %missionName;
  30.  
  31.    // Extract mission info from the mission file,
  32.    // including the display name and stuff to send
  33.    // to the client.
  34.    buildLoadInfo( %missionName );
  35.  
  36.    // Download mission info to the clients
  37.    %count = ClientGroup.getCount();
  38.    for( %cl = 0; %cl < %count; %cl++ ) {
  39.       %client = ClientGroup.getObject( %cl );
  40.       if (!%client.isAIControlled())
  41.          sendLoadInfoToClient(%client);
  42.    }
  43.  
  44.    // if this isn't the first mission, allow some time for the server
  45.    // to transmit information to the clients:
  46.    if( %isFirstMission || $Server::ServerType $= "SinglePlayer" )
  47.       loadMissionStage2();
  48.    else
  49.       schedule( $MissionLoadPause, ServerGroup, loadMissionStage2 );
  50. }
  51.  
  52. //-----------------------------------------------------------------------------
  53.  
  54. function loadMissionStage2() 
  55. {
  56.    // Create the mission group off the ServerGroup
  57.    echo("*** Stage 2 load");
  58.    $instantGroup = ServerGroup;
  59.  
  60.    // Make sure the mission exists
  61.    %file = $Server::MissionFile;
  62.    
  63.    if( !isFile( %file ) ) {
  64.       error( "Could not find mission " @ %file );
  65.       return;
  66.    }
  67.  
  68.    // Calculate the mission CRC.  The CRC is used by the clients
  69.    // to caching mission lighting.
  70.    $missionCRC = getFileCRC( %file );
  71.  
  72.    // Exec the mission, objects are added to the ServerGroup
  73.    exec(%file);
  74.    
  75.    // If there was a problem with the load, let's try another mission
  76.    if( !isObject(MissionGroup) ) {
  77.       error( "No 'MissionGroup' found in mission \"" @ $missionName @ "\"." );
  78.       schedule( 3000, ServerGroup, CycleMissions );
  79.       return;
  80.    }
  81.  
  82.    // Mission cleanup group
  83.    new SimGroup( MissionCleanup );
  84.    $instantGroup = MissionCleanup;
  85.    
  86.    // Construct MOD paths
  87.    pathOnMissionLoadDone();
  88.  
  89.    // Mission loading done...
  90.    echo("*** Mission loaded");
  91.    
  92.    // Start all the clients in the mission
  93.    $missionRunning = true;
  94.    for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ )
  95.       ClientGroup.getObject(%clientIndex).loadMission();
  96.  
  97.    // Go ahead and launch the game
  98.    onMissionLoaded();
  99.    purgeResources();
  100. }
  101.  
  102.  
  103. //-----------------------------------------------------------------------------
  104.  
  105. function endMission()
  106. {
  107.    if (!isObject( MissionGroup ))
  108.       return;
  109.  
  110.    echo("*** ENDING MISSION");
  111.    
  112.    // Inform the game code we're done.
  113.    onMissionEnded();
  114.  
  115.    // Inform the clients
  116.    for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
  117.       // clear ghosts and paths from all clients
  118.       %cl = ClientGroup.getObject( %clientIndex );
  119.       %cl.endMission();
  120.       %cl.resetGhosting();
  121.       %cl.clearPaths();
  122.    }
  123.    
  124.    // Delete everything
  125.    MissionGroup.delete();
  126.    MissionCleanup.delete();
  127.  
  128.    $ServerGroup.delete();
  129.    $ServerGroup = new SimGroup(ServerGroup);
  130. }
  131.  
  132.  
  133. //-----------------------------------------------------------------------------
  134.  
  135. function resetMission()
  136. {
  137.    echo("*** MISSION RESET");
  138.  
  139.    // Remove any temporary mission objects
  140.    MissionCleanup.delete();
  141.    $instantGroup = ServerGroup;
  142.    new SimGroup( MissionCleanup );
  143.    $instantGroup = MissionCleanup;
  144.  
  145.    //
  146.    onMissionReset();
  147. }
  148.